﻿/// <reference name="MicrosoftAjax.js"/>


Type.registerNamespace("Doo");

Doo.ClientXMLDataSource = function(element) {
    Doo.ClientXMLDataSource.initializeBase(this, [element]);
}
Doo.ClientXMLDataSource._loadok = function(response) {
    var xds = response.get_webRequest().get_userContext();

    var xml = xds.xmldoc = response.get_xml();
    var handlers = xds.xmlhandler;
    for (var i = 0; i < handlers.length; i++) {
        var dr = handlers.shift();
        if (dr && dr.succ && typeof (dr.succ) == 'function') dr.succ(xml);
    }
}
Doo.ClientXMLDataSource.prototype = {
    initialize: function() {
        Doo.ClientXMLDataSource.callBaseMethod(this, 'initialize');

        if (this.alias && this.alias.length > 0) {
            window[this.alias] = this;
        }

        // Add custom initialization here
        var request = new Sys.Net.WebRequest();
        request.set_url(this.xmlurl);
        request.add_completed(Doo.ClientXMLDataSource._loadok);
        request.set_userContext(this);
        request.invoke();
    },
    dispose: function() {
        //Add custom dispose actions here
        Doo.ClientXMLDataSource.callBaseMethod(this, 'dispose');
    },
    xmlurl: '',
    get_xmlurl: function() { return this.xmlurl; },
    set_xmlurl: function(value) { this.xmlurl = value; },
    alias: '',
    xmldoc: null, //xml文档
    get_xmldocument: function() { return this.xmldoc; },
    is_xmlavaliable: function() { return this.xmldoc != null; },
    xmlhandler: new Array(),
    set_xmlhandler: function(onsucc, onfail) {
        if (this.xmldoc == false) {
            onfail();
        }
        else if (this.xmldoc != null) {
            onsucc(this.xmldoc);
        }
        else {
            this.xmlhandler.push({ succ: onsucc, fail: onfail });
        }
    }
}
Doo.ClientXMLDataSource.registerClass('Doo.ClientXMLDataSource', Sys.UI.Control);
